#include <bits/stdc++.h>
using namespace std;

#define ONLINE_JUDGE

#ifndef ONLINE_JUDGE
#define dbg(x...)                             \
	{                                         \
		cerr << "\033[32;1m" << #x << " -> "; \
		err(x);                               \
	}
void err()
{
	cerr << "\033[39;0m" << endl;
}
template <typename T, typename... A>
void err(T a, A... x)
{
	cerr << a << ' ';
	err(x...);
}
#else
#define dbg(...)
#endif

typedef long long LL;

const int N = 1e5 + 5;

char s[N];
int n;

int main(int argc, char const *argv[])
{
	int t;
	cin >> t;
	for (int kk=0; kk<t; ++kk)
	{
		scanf("%d%s", &n, s);
		int ans = 0; bool update = true;
		for (int i=0; update; ++i)
		{
			update = false;
			for (int j=n-1; j>0; --j)
			{
				if (s[j] == 'P' && s[j-1] == 'A')
				{
					update = true;
					s[j] = 'A';
				}
			}
			if (update) ans = i + 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}